package com.home.cca.network;

import com.google.gson.Gson;
import com.home.cca.model.Goods;
import com.home.cca.model.Model;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import okhttp3.Request;

public class QueryGoodsRequest {

    private static final String URL = ApiClient.getBaseUrl() + "/SupplierManager/GoodsMgr/SupMatQueryGrid?name=&code=&classId=-1&state=-1&rows=10&type=-1&page=1&_=1733921610253";

    public static List<Goods> query() throws IOException {
        // 构建请求
        Request request = new Request.Builder()
                .url(URL)
                .get()
                .addHeader("Accept", "application/json, text/javascript, */*; q=0.01")
                .addHeader("Accept-Language", "zh-CN,zh;q=0.9")
                .addHeader("Connection", "keep-alive")
                .addHeader("Cookie", ApiClient.getCookie())
                .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36")
                .addHeader("X-Requested-With", "XMLHttpRequest")
                .build();

        Gson gson = new Gson();
        String execute = ApiClient.execute(request);
        Model model = gson.fromJson(execute, Model.class);
        if (model.getRows() == null && model.getRows().isEmpty()) {
            throw new IOException("查询商品信息异常");
        }
        return model.getRows().stream()
                .map(it -> gson.fromJson(gson.toJson(it), Goods.class))
                .collect(Collectors.toList());
    }
}
